package com.alvinlkk.basic.chapter21.waxomatic2;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Meal {
    private final int orderNum;
    
    Meal(int orderNum) {
        this.orderNum = orderNum;
    }
    
    @Override
    public String toString() {
        return "Meal = " + orderNum;
    }
}

class WaitPerson implements Runnable {
    
    private Restaurant2 restaurant;
    
    Lock lock = new ReentrantLock();
    
    Condition condition = lock.newCondition();
    
    public WaitPerson(Restaurant2 restaurant) {
        this.restaurant = restaurant;
    }

    @Override
    public void run() {
        try {
            while(!Thread.interrupted()) {
                lock.lock();
                try {
                    while(restaurant.meal == null) {
                        condition.await();
                    }
                } finally {
                    lock.unlock();
                }
                    
                    
               System.out.println("WaitPerson got " + restaurant.meal);
                
               restaurant.chef.lock.lock();
               try {
                   restaurant.meal = null;
                   restaurant.chef.condition.signalAll();
               } finally {
                restaurant.chef.lock.unlock();
               }
               
               restaurant.boy.lock.lock();
               try {
                   restaurant.meal = null;
                   restaurant.boy.condition.signalAll();
               } finally {
                restaurant.boy.lock.unlock();
               }
                
            }
        } catch (InterruptedException e) {
            System.out.println("WaitingPerson interrupted....");
        }
        
    }
    
}

class Chef implements Runnable {
    
    Lock lock = new ReentrantLock();
    
    Condition condition = lock.newCondition();

    private Restaurant2 restaurant;
    
    private int count = 0;
    
    public Chef(Restaurant2 restaurant) {
        this.restaurant = restaurant;
    }
    
    @Override
    public void run() {
        try {
            while (!Thread.interrupted()) {
                lock.lock();
                try {
                    while(restaurant.meal != null) {
                        condition.await();
                    }
                } finally {
                    lock.unlock();
                }
                
                if(++count == 10) {
                    System.out.println("out of food closing");
                    restaurant.exec.shutdownNow();
                    return ;
                }
                System.out.println("ORder up !");
                
                restaurant.waitPerson.lock.lock();
                try {
                    restaurant.meal = new Meal(count);
                    restaurant.waitPerson.condition.signalAll();
                } finally {
                    restaurant.waitPerson.lock.unlock();
                }
                
                TimeUnit.MILLISECONDS.sleep(100);
            }
            
        } catch (InterruptedException e) {
            System.out.println("Chef interrupted");
        }
    }
    
}

//在上菜之后，waitPerson通知BusyBoy清理
class BusyBoy implements Runnable {
    
    Lock lock = new ReentrantLock();
    
    Condition condition = lock.newCondition();

    @Override
    public void run() {
        
        try {
            while (!Thread.interrupted()) {
                lock.lock();
                try {
                    condition.await();
                } finally {
                    lock.unlock();
                }
                System.out.println("Busy Boy clean---");
            }
            
        } catch (Exception e) {
            System.out.println("BusyBoy interrupted");
        }
        
    }
    
}

public class Restaurant2 {
    
    Meal meal;
    
    WaitPerson waitPerson = new WaitPerson(this);
    
    Chef chef = new Chef(this);
    
    BusyBoy boy = new BusyBoy();
    
    ExecutorService exec = Executors.newCachedThreadPool();
    
    
    
    public Restaurant2() {
        exec.execute(waitPerson);
        exec.execute(chef);
        exec.execute(boy);
    }
    
    public static void main(String[] args) {
        new Restaurant2();
    }
}
